<?php
	function GetSampleDetails($sample_key, $database, $host, $login, $loginPassword){
		$returnValue = array();
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT * FROM sample_details WHERE sample_key = ".$sample_key;
		$result = mysql_query($query)or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row);
		}
		mysql_close($con);
		return $returnValue;
	}
	
	function GetMaxDate($sample_key, $database, $host, $login, $loginPassword){
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT MAX(date_finalized) AS MAX FROM sample_details WHERE sample_key = ".$sample_key;
		$result = mysql_query($query)or die(mysql_error());
		mysql_close($con);
		return $result[0];
	}
	
	function GetDistinctSampleTypeEntry($sample_key, $database, $host, $login, $loginPassword){
		$returnValue = array();
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT DISTINCT sample_type FROM sample_entry WHERE sample_key = ".$sample_key;
		$result = mysql_query($query)or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row[0]);
		}
		mysql_close($con);
		return $returnValue;
	}
	
	function GetDistinctSampleAnalysisEntry($sample_key, $database, $host, $login, $loginPassword){
		$returnValue = array();
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT DISTINCT analysis_required FROM sample_entry WHERE sample_key = ".$sample_key;
		$result = mysql_query($query)or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row[0]);
		}
		mysql_close($con);
		return $returnValue;
	}
	
	function GetSampleEntry($sample_key, $database, $host, $login, $loginPassword){
		$returnValue = array();
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT * FROM sample_entry WHERE sample_key = ".$sample_key;
		$result = mysql_query($query)or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row);
		}
		mysql_close($con);
		return $returnValue;
	}
	
	function GetSampleEntryNumbers($sample_key, $database, $host, $login, $loginPassword){
		$returnValue = array();
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT DISTINCT casl_no_year, MIN(casl_no_sample) AS MIN, MAX(casl_no_sample) AS MAX FROM sample_entry WHERE sample_key = ".$sample_key;
		$result = mysql_query($query)or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row);
		}
		mysql_close($con);
		return $returnValue;
	}
	
	function GetAdministratorName($administrator, $database, $host, $login, $loginPassword){
		$returnValue = array();
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT name FROM users WHERE username =\"".$administrator."\"";
		$result = mysql_query($query)or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row);
		}
		mysql_close($con);
		return $returnValue;
	}
	
	function GetInformation($sample_key, $database, $host, $login, $loginPassword){
		$returnValue = array();
		$con = mysql_connect($host,$login,$loginPassword) or die('Could not connect: ' . mysql_error());
		mysql_select_db($database) or die(mysql_error());
		$query = "SELECT sample_code, casl_no_year, casl_no_sample, analysis_required, mean, standard_deviation, unit FROM sample_entry s, analysis_result a WHERE s.sample_key = ".$sample_key." AND a.casl_year=s.casl_no_year AND a.casl_sample=s.casl_no_sample";
		$result = mysql_query($query)or die(mysql_error());
		while ($row = mysql_fetch_row($result, MYSQL_NUM)){
			array_push($returnValue, $row);
		}
		mysql_close($con);
		return $returnValue;
	}
	
	//This function gathers the unit from the database to be used in the reoprt.
	function GetUnit($casl_no_year, $casl_no_sample, $database, $host, $login, $loginPassword){
		//Create connection.
		$con = mysql_connect($host,$login,$loginPassword) or 
			die('Could not connect: ' . mysql_error());
		//Select database.
		mysql_select_db($database) or die(mysql_error());
		//Execute query.
		$query = "SELECT unit FROM analysis_result WHERE casl_year='$casl_no_year' AND casl_sample='$casl_no_sample'";
		$result = mysql_query($query)or die(mysql_error());
		$row = mysql_fetch_array($result);
		//Close connection.
		mysql_close($con);
		//Return first index.
		return $row[0];
	}
	
	//This function updates the unit.
	function UpdateUnit($casl_no_year, $casl_no_sample, $unit, $database, $host, $login, $loginPassword){
		//Create connection.
		$con = mysql_connect($host,$login,$loginPassword) or 
			die('Could not connect: ' . mysql_error());
		//Select database.
		mysql_select_db($database) or die(mysql_error());
		//Execute query.
		$query = "UPDATE analysis_result SET unit = '$unit' WHERE casl_year='$casl_no_year' AND casl_sample='$casl_no_sample'";
		$result = mysql_query($query);
		//Close connection.
		mysql_close($con);
		//Check results if updated.
		if($result == FALSE){
			return false;	
		}else{
			return true;
		}	
	}